#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from subprocess import call
import logging

# Keywords to search
KEYWORDS = ['catch', 'exception']

# Fives open source Android projects from github/google code archive, they are:
#  + MyTracks, which is dead since 2016
#  + barcodescanner, whose source and issues are hold on github
#  + sipdroid, exported to github
#  + android-xbmcremote, old issues are on google code and new issues are on github
#  + keepassdroid, same as above
#
PROJECTS = {
    'google code': ['mytracks', 'android-xbmcremote', 'keepassdroid'],
    'github': [
        'dm77/barcodescanner', 'i-p-tel/sipdroid', 'freezy/android-xbmcremote',
        'bpellin/keepassdroid'
    ]
}

# LOGLEVEL
LOGLEVEL = logging.INFO

# Spider
SPIDER = 'issue'


def loglevel(level):
    return {
        logging.INFO: 'INFO',
        logging.DEBUG: 'DEBUG',
        logging.WARN: 'WARN'
    }[level]


def crawl_issue(category, repo, keywords, output, loglevel):
    command = [
        'scrapy', 'crawl', '-a', 'category=%s' % category, '-a',
        'repo=%s' % repo, '-a', 'keywords=%s' % ','.join(keywords), '-o',
        output, SPIDER
    ]
    if loglevel is not None:
        command.append('--loglevel=' + loglevel)

    logging.info('executing command: %s' % (' '.join(command)))
    call(command)


def replace_characters(chs, new, text):
    for ch in chs:
        text = text.replace(ch, new)
    return text


def main():
    for category, repos in PROJECTS.items():
        for repo in repos:
            output = 'results/%s_%s.csv' % (category, repo)
            output = replace_characters(' /-', '_', output)
            crawl_issue(category, repo, KEYWORDS, output, loglevel(LOGLEVEL))


# Run
main()
